import request from "../util/request";
import store from '../store'

export default {
    sendPostTrx(inTrx, ignoreErrorCode) {
        if (ignoreErrorCode && ignoreErrorCode.length > 0) {
            store.commit('ErrorCode/setIgnoreErrorCode', ignoreErrorCode)
        } else {
            store.commit('ErrorCode/setIgnoreErrorCode', [])
        }
        inTrx = this.trimInTrx(inTrx);
        inTrx.evtUsr = window.localStorage.getItem("userIdMes");

        return request({
            url: `/fullsemi/sendMsg/${inTrx.trxId}/${inTrx.actionFlg}`,
            method: "post",
            data: {
                trxId: inTrx.trxId,
                strInMsg: JSON.stringify(inTrx),
            }
        });
    },

    getBisData(bisData, callback) {
        let inTrx = {
            trxId: 'bisData',
            actionFlg: 'Q',
            iary: [bisData]
        }

        inTrx.evtUsr = window.localStorage.getItem("userIdMes")

        request({
            url: `/fullsemi/sendMsg/${inTrx.trxId}/${inTrx.actionFlg}`,
            method: "post",
            data: {
                trxId: inTrx.trxId,
                strInMsg: JSON.stringify(inTrx),
            }
        }).then(({data}) => {
            if (data.rtnCode !== '0000000') {
                return
            }
            callback(data.oary)
        });
    },

    trimInTrx(inTrxObject) {
        for (let item in inTrxObject) {
            let val = inTrxObject[item];
            if (typeof val === 'string') {
                inTrxObject[item] = val.trim();
            } else if (typeof val === 'object') {
                inTrxObject[item] = this.trimInTrx(val);
            }
        }
        return inTrxObject;
    },
};
